package com.baibei.order.bill.orderrecord;

import android.content.Context;

import com.baibei.basic.BasicPresenterImpl;
import com.baibei.basic.module.observer.PageDataObservable;
import com.baibei.basic.module.observer.PageObservable;
import com.baibei.ebec.sdk.ApiFactory;
import com.baibei.ebec.sdk.IOrderApi;
import com.baibei.model.OrderInfo;
import com.baibei.model.SumClosedOrderInfo;
import com.baibei.sdk.ApiDefaultObserver;

import java.util.List;

import io.reactivex.Observable;

/**
 * 订货记录表现层实现
 * Created by shenminjie on 2017/8/17.
 * mail:shenminjie@100bei.com
 */

public class OrderRecordPresenterImpl extends BasicPresenterImpl<OrderRecordContract.OrderRecordView> implements OrderRecordContract.Presenter {

    private IOrderApi mOrderApi;
    private PageObservable<OrderInfo> mPageObservable;
    private List<OrderInfo> mOrderLists;

    public OrderRecordPresenterImpl(Context context, OrderRecordContract.OrderRecordView view) {
        super(context, view);
        mOrderApi = ApiFactory.getInstance().getOrderApi();
        mPageObservable = new PageDataObservable<OrderInfo>(mView) {
            @Override
            protected Observable<List<OrderInfo>> onCreateObserver(int page) {
                return createObservable(mOrderApi.getOrderRecordList(page));
            }

            @Override
            protected void onLoadDataComplete(List<OrderInfo> dataList) {
                super.onLoadDataComplete(dataList);
                mOrderLists = dataList;
                mView.hideRefreshLayout();
            }

            @Override
            protected void onNoMoreData() {
                super.onNoMoreData();
                mView.hideRefreshLayout();
            }
        };
    }

    @Override
    public void start() {
        super.start();
        mPageObservable.start();
    }

    @Override
    public void loadMore() {
        mPageObservable.loadMore();
    }

    @Override
    public void getOrderSum() {
        createObservable(mOrderApi.getSumClosedOrder()).subscribe(new ApiDefaultObserver<SumClosedOrderInfo>() {
            @Override
            protected void onError(String s) {
                if (mView != null) {
                    mView.onLoadOrderSumFailed(s);
                }
            }

            @Override
            protected void accept(SumClosedOrderInfo info) {
                if (mView != null) {
                    mView.onLoadOrderSum(info);
                }
            }
        });
    }
}
